﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Hangjing.Model;
using Hangjing.SQLServerDAL;
using Hangjing.Common;

namespace Html5
{
    public partial class submitexpressorder : PageBase
    {
        ExpressOrder dal = new ExpressOrder();
        protected void Page_Load(object sender, EventArgs e)
        {
            //获取post信息，提交订单
            if (Request.HttpMethod.ToUpper() == "POST")
            {
                add_Click();
            }

            //判断是否 登录
            ECustomerInfo model = UserHelp.GetUser();

            if (model == null)
            {
                //Response.Redirect("login.aspx?returnurl=" + Server.UrlEncode(Request.Url.ToString()));
            }
            else
            {
                model = new ECustomer().GetModel(model.DataID);
                hfuid.Value = model.DataID.ToString();
                //lbmymoney.InnerText = model.Usermoney + "元";
            }
        }

        /// <summary>
        /// submit
        /// </summary>
        public void add_Click()
        {
            ExpressOrderInfo info = new ExpressOrderInfo();
            expressinfo expressmodel = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize<expressinfo>(Request.Form["tbexpressinfo"]);

            CustorderInfo infoaddress = new CustorderInfo();
            //发件人
            infoaddress.OrderRcver = WebUtility.InputText(expressmodel.tbUserName);
            infoaddress.AddressText = WebUtility.InputText(expressmodel.tbAddress + "," + expressmodel.tbAddressdetail);
            infoaddress.OrderComm = WebUtility.InputText(expressmodel.tbTel);

            //收件人
            infoaddress.OrderAddress = WebUtility.InputText(expressmodel.tbcallmsg);
            infoaddress.shopauthuser = WebUtility.InputText(expressmodel.tbReveVar);
            infoaddress.OrderAddrEx = WebUtility.InputText(expressmodel.tbOorderid + "," + expressmodel.tbOorderiddetail);


            string food = "["+(Convert.ToInt32(expressmodel.tbcallcount) == 0?"送":"买") +"]"+WebUtility.InputText(expressmodel.tbInve2);
 
        
            infoaddress.paymode = Convert.ToInt32(WebUtility.InputText(Request.Form["ddlpaymode"]));

            switch (infoaddress.paymode)
            {
                case 5:
                    break;
                case 11:
                    infoaddress.paymode = 4;//发件人支付，收件人支付都是货到付款
                    food += "(发件人支付) ";
                    break;
                case 12:
                    food += "(收件人支付) ";
                    infoaddress.paymode = 4;//发件人支付，收件人支付都是货到付款
                    break;
                default:
                    break;
            }

            infoaddress.OrderAttach = food + WebUtility.InputText(expressmodel.tbRemark);


            infoaddress.SendTime = Convert.ToDateTime(WebUtility.InputText(expressmodel.tbSentTime));


            string uc = WebUtility.FixgetCookie("openid");
            if (uc == null)
            {
                uc = "";
            }

            infoaddress.tempcode = uc;
            infoaddress.CustomerName = "";
            infoaddress.UserId = base.user.DataID;
            infoaddress.fromweb = "7";
            infoaddress.ReveVar1 = "0";


            info.ulat = WebUtility.InputText(expressmodel.hidflat);
            info.ulng = WebUtility.InputText(expressmodel.hidflng);
            info.shoplat = WebUtility.InputText(expressmodel.hidtlat);
            info.shoplng = WebUtility.InputText(expressmodel.hidtlng);


            infoaddress.ReveVar2 = "{'ulat':'" + info.ulat + "','ulng':'" + info.ulng + "','slat':'" + info.shoplat + "','slng':'" + info.shoplng + "'}";
            infoaddress.OrderSums = Convert.ToDecimal(expressmodel.tbTotalPrice);
            infoaddress.Advance = 0; //Convert.ToDecimal(base._httpReuqest["tbAdvance"]);
            infoaddress.shopdiscountmoney = Convert.ToDecimal(expressmodel.hidsendfee);
            infoaddress.shoptip = 0;

            infoaddress.OldPrice = infoaddress.OrderSums;
            infoaddress.cityid = Convert.ToInt32(expressmodel.cityid);
            infoaddress.TogoId = 0;
            infoaddress.P2Sign = "";
            infoaddress.paytime = Convert.ToDateTime("1970-1-1");
            infoaddress.IsShopSet = 1;
            infoaddress.ReveDate1 = DateTime.Now;
            infoaddress.cardpay = Convert.ToDecimal(expressmodel.hiddistance);
            infoaddress.cardids = "";
            infoaddress.Commission = 0;
            infoaddress.settlecycle = 0;
         
            infoaddress.Mergeorderremark = "";
            infoaddress.SystemUserId = 0;
            infoaddress.InUse = "";
            infoaddress.deliverheaderid = 0;
            infoaddress.OldStatus = 0;

            Custorder orderdal = new Custorder();
  
            ECustomerInfo user = UserHelp.GetUser();
            if (user != null)
            {
                info.UserID = user.DataID;
                info.CustomerName = user.Name;

                if (user.DataID > 0)
                {
                    ECustomerInfo einfo = new ECustomer().GetModel(user.DataID);
                    if (einfo != null)
                    {
                        decimal useroney = einfo.Usermoney;
                        if (info.PayMode == 3)
                        {
                            string PayPassword = WebUtility.InputText(Request.Form["tbpaypwd"]);
                            if (useroney == 0)
                            {
                                Response.Redirect("submitexpressorder.aspx?id=" + 0 + "&msg=2");//您的账户余额为0，不能选择账户余额支付
                                return;
                            }
                            //可以支付部分金额
                            if (useroney < info.sendmoney)
                            {
                                Response.Redirect("submitexpressorder.aspx?id=" + 0 + "&msg=6");//余额不足，请选择其他支付方式
                                return;
                            }

                            if (WebUtility.GetMd5(PayPassword) != user.PayPassword)
                            {
                                Response.Redirect("expressOrderSuccess.aspx?id=" + 0 + "&msg=3");//支付密码错误，请重新输入
                                return;
                            }
                        }
                    }

                }
            }
            else
            {
                info.UserID = 0;
                info.CustomerName = "";
            }

            if (orderdal.AddTBOrder(infoaddress) > 0)
            {
                Response.Redirect("expressOrderSuccess.aspx?orderid=" + infoaddress.orderid);

            }
            else
            {
                Response.Redirect("expressOrderSuccess.aspx?id=" + 0 + "&msg=1");//失败
            }

           

        }

    }
}